﻿using System;
using System.IO;
using System.Text;

namespace XNET.IO
{
    /// <summary>
    /// 文件操作类
    /// </summary>
    public class XFile
    {
        /// <summary>
        /// 获取文件MD5码
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        public static string GetMD5FromFile(string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("获取文件MD5码错误" + ex.Message);
            }
        }

        /// <summary>
        /// 获取文件SHA1码
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        public static string GetSHA1FromFile(string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
                byte[] retVal = sha1.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("获取文件SHA1码错误" + ex.Message);
            }
        }

        /// <summary>
        /// 获取文件SHA256码
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        public static string GetSHA256FromFile(string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                System.Security.Cryptography.SHA256CryptoServiceProvider sha256 = new System.Security.Cryptography.SHA256CryptoServiceProvider();
                byte[] retVal = sha256.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("获取文件SHA256码错误" + ex.Message);
            }
        }

        /// <summary>
        /// 获取文件SHA384码
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        public static string GetSHA384FromFile(string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                System.Security.Cryptography.SHA384CryptoServiceProvider sha384 = new System.Security.Cryptography.SHA384CryptoServiceProvider();
                byte[] retVal = sha384.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("获取文件SHA384码错误" + ex.Message);
            }
        }

        /// <summary>
        /// 获取文件SHA512码
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        public static string GetSHA512FromFile(string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                System.Security.Cryptography.SHA512CryptoServiceProvider sha512 = new System.Security.Cryptography.SHA512CryptoServiceProvider();
                byte[] retVal = sha512.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("获取文件SHA512码错误" + ex.Message);
            }
        }
    }
}
